<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace app\filters;

use Yii;
use yii\base\ActionEvent;
use yii\base\ActionFilter;
use yii\base\Behavior;
use yii\web\Controller;
use yii\web\MethodNotAllowedHttpException;

class CheckAccessFilter extends ActionFilter
{
    public $checkAccess;

    /**
     * Declares event handlers for the [[owner]]'s events.
     * @return array events (array keys) and the corresponding event handler methods (array values).
     */
    public function events()
    {
        return [Controller::EVENT_BEFORE_ACTION => 'beforeAction'];
    }

    /**
     * @param ActionEvent $event
     * @return bool
     */
    public function beforeAction($action)
    {
        if ($this->checkAccess) {
            call_user_func($this->checkAccess, $action->id);
        }
        return true;
    }
}
